package com.foreverwin.mes.so.controller;

import com.foreverwin.mes.core.base.FrontPage;
import com.foreverwin.mes.core.base.AjaxResult;
import com.foreverwin.mes.core.utils.CommonMethods;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.foreverwin.mes.so.service.ShopOrderGenHisService;
import com.foreverwin.mes.so.model.ShopOrderGenHis;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 *
 * @author Syngna
 * @since 2020-03-05
 */
@Controller
@RequestMapping("/shopOrderGenHiss")
public class ShopOrderGenHisController {
    private final Logger logger = LoggerFactory.getLogger(ShopOrderGenHisController.class);

    @Autowired
    public ShopOrderGenHisService shopOrderGenHisService;

    /**
    * 根据id查询
    *
    * @param id 主键
    * @return
    */
    @ResponseBody
    @GetMapping("/{id:.+}")
    public AjaxResult getShopOrderGenHisById(@PathVariable String id) {
        ShopOrderGenHis result;
        try {
            result = shopOrderGenHisService.selectById(id);
        } catch (Exception e) {
            logger.error("getShopOrderGenHisById -=- {}", e.toString());
            return AjaxResult.error( e.getMessage() );
        }
        return AjaxResult.success(result);
    }

    /**
     * 查询所有数据
     *
     * @return
     */
    @ResponseBody
    @GetMapping("")
    public AjaxResult getShopOrderGenHisList(ShopOrderGenHis shopOrderGenHis){
        List<ShopOrderGenHis> result;
        try {
            EntityWrapper<ShopOrderGenHis> EntityWrapper = new EntityWrapper<>();
            EntityWrapper.setEntity(shopOrderGenHis);
            result = shopOrderGenHisService.selectList(EntityWrapper);
        } catch (Exception e) {
            logger.error("getShopOrderGenHisList -=- {}", e.toString());
            return AjaxResult.error( e.getMessage() );
        }
        return AjaxResult.success(result);
    }

    /**
     * 分页查询数据
     *
     * @param frontPage  分页信息
     * @return
     */
    @ResponseBody
    @GetMapping("/getShopOrderGenHisPageList")
    public AjaxResult getShopOrderGenHisPageList(FrontPage<ShopOrderGenHis> frontPage, ShopOrderGenHis shopOrderGenHis){
        Page result;
        try {
            String site = CommonMethods.getSite();
            shopOrderGenHis.setSite(site);
            EntityWrapper<ShopOrderGenHis> EntityWrapper = new EntityWrapper<>();
            EntityWrapper.setEntity(shopOrderGenHis);
            if (frontPage.getGlobalQuery() != null && !"".equals(frontPage.getGlobalQuery().trim())) {
                //TODO modify global query
                EntityWrapper
                    .like("handle", frontPage.getGlobalQuery())
                    .or().like("site", frontPage.getGlobalQuery())
                    .or().like("shopOrder", frontPage.getGlobalQuery())
                    .or().like("itemBo", frontPage.getGlobalQuery())
                    .or().like("transactionBatch", frontPage.getGlobalQuery())
                    .or().like("createUser", frontPage.getGlobalQuery())
        ;
            }
            result = shopOrderGenHisService.selectPage(frontPage.getPagePlus(), EntityWrapper);
        } catch (Exception e) {
            logger.error("getShopOrderGenHisPageList -=- {}", e.toString());
            return AjaxResult.error( e.getMessage() );
        }
        return AjaxResult.success(result);
    }

    /**
     * 保存和修改公用
     * @param shopOrderGenHis  传递的实体
     * @return  null 失败  实体成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/_save")
    public AjaxResult shopOrderGenHisSave(@RequestBody ShopOrderGenHis shopOrderGenHis) {
        String site = CommonMethods.getSite();
        String user = CommonMethods.getUser();
        int count;
        try {
            count = shopOrderGenHisService.insertOrUpdate(shopOrderGenHis) ? 1 : 0;
        } catch (Exception e) {
            logger.error("shopOrderGenHisSave -=- {}", e.toString());
            return AjaxResult.error( e.getMessage() );
        }
        return AjaxResult.success(count == 0 ? null : shopOrderGenHis);
    }

    /**
     * 根据id删除对象
     * @param id  实体ID
     * @return 0 失败  1 成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.DELETE, value = "/{id:.+}")
    public AjaxResult shopOrderGenHisDelete(@PathVariable("id") String id){
        int count;
        try {
            count = shopOrderGenHisService.deleteById(id) ? 1 : 0;
        } catch (Exception e) {
            logger.error("shopOrderGenHisDelete -=- {}", e.toString());
            return AjaxResult.error( e.getMessage() );
        }
        return AjaxResult.success("count: " + count);
    }

    /**
     * 批量删除对象
     * @param ids 实体集合ID
     * @return  0 失败  1 成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/_batchDelete")
    public AjaxResult removeByIds(List<String> ids){
        int count;
        try {
            count = shopOrderGenHisService.deleteBatchIds(ids) ? 1 : 0;
        } catch (Exception e) {
            logger.error("shopOrderGenHisBatchDelete -=- {}", e.toString());
            return AjaxResult.error( e.getMessage() );
        }
        return AjaxResult.success("count: " + count);
    }
}